﻿using System;
using System.Collections.Generic;
using NUnit.Framework;
using Xenta.Operations;
using Xenta.Utils;

namespace Xenta.Tests.Operations
{
    [TestFixture]
    public class CatalogItemDimensionalSpecTests : OperationTest
    {
        protected override IEnumerable<Type> ModelMappings
        {
            get
            {
                return NHMappings.ALL;
            }
        }

        [TestCase("sku", 1, 2, 3, 4)]
        public void Can_CRUD(string sku, decimal weight, decimal length, decimal height, decimal width)
        {
            var spec = Processor.Execute(new CreateCatalogItemDimensionalSpec
            {
                ItemID = Processor.Execute(new CreateCatalogItem
                {
                    SupplierID = Processor.Execute(new CreateCatalogSupplier()).Result.EntityID,
                }).Result.EntityID,
                SKU = sku,
                Weight = weight,
                Length = length,
                Height = height,
                Width = width
            }).Result;

            Assert.NotNull(spec);
            spec.SKU.AreEqualIgnoringCase(sku);
            spec.Weight.AreEqual(weight);
            spec.Length.AreEqual(length);
            spec.Height.AreEqual(height);
            spec.Width.AreEqual(width);
            spec.AreEqual(Processor.Execute(new GetCatalogItemSpec
            {
                EntityID = spec.EntityID
            }).Result);

            sku = sku.Reverse();
            weight = weight * 2;
            length = length * 2;
            height = height * 2;
            width = width * 2;
            var upd_spec = Processor.Execute(new UpdateCatalogItemDimensionalSpec
            {
                EntityID = spec.EntityID,
                SKU = sku,
                Weight = weight,
                Length = length,
                Height = height,
                Width = width
            }).Result;

            Assert.NotNull(upd_spec);
            upd_spec.AreEqual(spec);
            upd_spec.SKU.AreEqualIgnoringCase(sku);
            upd_spec.Weight.AreEqual(weight);
            upd_spec.Length.AreEqual(length);
            upd_spec.Height.AreEqual(height);
            upd_spec.Width.AreEqual(width);

            Processor.Execute(new DeleteCatalogItemSpec
            {
                EntityID = spec.EntityID
            });
        }

        [TestCase(-1, 1, 1, 1)]
        [TestCase(1, -1, 1, 1)]
        [TestCase(1, 1, -1, 1)]
        [TestCase(1, 1, 1, -1)]
        public void Cannot_Create_If_OneOfDimension_Negative(decimal weight, decimal length, decimal height, decimal width)
        {
            Assert.Throws<ApplicationException>(() => Processor.Execute(new CreateCatalogItemDimensionalSpec
            {
                Weight = weight,
                Length = length,
                Height = height,
                Width = width
            }));
        }

        [TestCase(-1, 1, 1, 1)]
        [TestCase(1, -1, 1, 1)]
        [TestCase(1, 1, -1, 1)]
        [TestCase(1, 1, 1, -1)]
        public void Cannot_Update_If_OneOfDimension_Negative(decimal weight, decimal length, decimal height, decimal width)
        {
            Assert.Throws<ApplicationException>(() => Processor.Execute(new UpdateCatalogItemDimensionalSpec
            {
                Weight = weight,
                Length = length,
                Height = height,
                Width = width
            }));
        }
    }
}
